#!/bin/bash
Date=$(date +%F" "%T)
#users="15305713256"
users="15305713256,057187396715"
Mail="a30402104@126.com"
IP=`ifconfig eth0 | awk '/inet addr:/{print $2}' | awk -F: '{print $2}'`
sendsms_bin="/etc/keepalived/SendSmsMail.py"
case "$1" in
        master)
                echo "$Date: switch role to master" >> /etc/keepalived/SendSmsMail.log
                /etc/init.d/keepalived start
                sleep 2
                /etc/init.d/kamailio start
                python $sendsms_bin "$users" "keepalived_warning: $Date IP:$IP the GW_kamailio service is switched"
                exit 0
        ;;
        backup)
                echo "$Date: switch role to backup" >> /etc/keepalived/SendSmsMail.log
                /etc/init.d/kamailio stop
                sleep 2
                exit 0
        ;;
        fault)
                echo "$Date: switch role failed !!! " >> /etc/keepalived/SendSmsMail.log
                /etc/init.d/kamailio stop
                python $sendsms_bin "$users" "keepalived_warning: $Date IP:$IP the GW_kamailio service is fault,check it now!"
                sleep 2
                exit 0
        ;;
        *)
                echo 'Usage:`basename $0` {master|backup|fault}'
                exit 1
        ;;
esac
